package com.tecniplast.selene.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.tecniplast.selene.common.util.MessageUtils;
import com.tecniplast.selene.dao.AutoReplyDao;
import com.tecniplast.selene.pojo.Message;
import com.tecniplast.selene.pojo.Music;
import com.tecniplast.selene.pojo.MusicMessage;
import com.tecniplast.selene.pojo.NewDetail;
import com.tecniplast.selene.pojo.News;
import com.tecniplast.selene.pojo.NewsMessage;
import com.tecniplast.selene.pojo.TextMessage;
import com.tecniplast.selene.service.AutoReplyService;
import com.thoughtworks.xstream.XStream;
/**
 * syscodeList 代码表信息
1text  2news  3image  4voice
5music  6video  7shortvideo  8link
9location  10event  11subscribe  12unsubscribe
13SCAN  14CLICK  15VIEW  16LOCATION
 */
@Service("autoReplyService") 
public class AutoReplyServiceImpl implements AutoReplyService {
	@Resource
	 private AutoReplyDao autoReplyDao;
	 
	/**
	 输入返回结果
	 */
	@Override
	public String msgDeal(Map<String, String> map) {
		String  returnStr = "";
		//获取微信平台传递参数
		String fromUserName = map.get("FromUserName");
		String toUserName = map.get("ToUserName");
		String content = map.get("Content");
		//获取代码表参数信息
		if("1".contains(content)){
			//公司简介
			TextMessage text = new TextMessage();
			text.setFromUserName(toUserName);
			text.setToUserName(fromUserName);
			text.setMsgType(MessageUtils.MESSAGE_TEXT);
			text.setCreateTime(new Date().getTime());
			String info = autoReplyDao.queryMsgById(Long.parseLong(content));
			text.setContent(info);
			returnStr = MessageUtils.textMessageToXml(text);
		}else if("2".contains(content)){
			List<News> newsList = new ArrayList<News>();
			NewsMessage newsMessage = new NewsMessage();
			//获取图文信息
			Message msg = autoReplyDao.queryNewsMainMsgById(Long.parseLong(content));
			String articlesid = msg.getArticlesid();
			List<NewDetail> details = autoReplyDao.queryNewsDetailMsgById(Long.parseLong(articlesid));
			for(NewDetail nd : details){
				News news = new News();
				news.setTitle(nd.getTitle());
				news.setDescription(nd.getDescription());
				news.setPicUrl(nd.getPicurl());
				news.setUrl(nd.getUrl());
				newsList.add(news);
			}
			newsMessage.setToUserName(fromUserName);
			newsMessage.setFromUserName(toUserName);
			newsMessage.setCreateTime(new Date().getTime());
			newsMessage.setMsgType(msg.getMsgtype());
			newsMessage.setArticles(newsList);
			newsMessage.setArticleCount(newsList.size());
			returnStr = newsMessageToXml(newsMessage);
		}else if("3".equals(content)){
			Message msg = autoReplyDao.queryNewsMainMsgById(Long.parseLong(content));
			MusicMessage mm = new MusicMessage();
			mm.setToUserName(fromUserName);
			mm.setFromUserName(toUserName);
			mm.setCreateTime(new Date().getTime());
			mm.setMsgType(msg.getMsgtype());
			Music m = new Music();
			m.setTitle(msg.getTitle());
			m.setDescription(msg.getDescription());
			m.setMusicUrl(msg.getMsicurl());
			m.setHQMusicUrl(msg.getHqmusicurl());
			m.setThumbMediaId(msg.getThumbmediamid());
			mm.setMusic(m);
			returnStr = musicMessageToXml(mm);
		}
		return returnStr;
	}
	
	/**
	 * 图文消息转为xml
	 * @param newsMessage
	 * @return
	 */
	public static String newsMessageToXml(NewsMessage newsMessage){
		XStream xstream = new XStream();
		xstream.alias("xml", newsMessage.getClass());
		xstream.alias("item", new News().getClass());
		return xstream.toXML(newsMessage);
	}
	
	/**
	 * 图文消息转为xml
	 * @param newsMessage
	 * @return
	 */
	public static String musicMessageToXml(MusicMessage musicMessage){
		XStream xstream = new XStream();
		xstream.alias("xml", musicMessage.getClass());
		xstream.alias("Music", new Music().getClass());
		return xstream.toXML(musicMessage);
	}
}
